package com.hjimi.ski.presenter;

import android.util.Log;

import com.hjimi.ski.bean.IsBindBean;
import com.hjimi.ski.callback.BaseCallback;
import com.hjimi.ski.config.UrlConfig;
import com.hjimi.ski.iView.IsBandIView;
import com.hjimi.ski.model.base.DataModel;
import com.hjimi.ski.model.base.ModelToken;
import com.hjimi.ski.utils.JsonParseUtil;
import com.hjimi.ski.utils.ToastUtils;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 是否绑定
 * Created By 孙岩
 * on 2023/2/11
 */
public class IsBandPresenter extends BasePresenter<IsBandIView>{
    @SuppressWarnings("unchecked")
    public void getIsBand(String id){
        if(isViewAttached()){
            String url = UrlConfig.isBind + id;
            DataModel.request(ModelToken.GET_REQUEST)
                    .url(url)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if(obj.getInt("code")==200||obj.getInt("code")==201){
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    IsBindBean.DataBean bean = new IsBindBean.DataBean(
                                            JsonParseUtil.getInt(object,"id"),
                                            JsonParseUtil.getStr(object,"nickname"),
                                            JsonParseUtil.getStr(object,"avatar"),
                                            JsonParseUtil.getStr(object,"last_date")
                                    );

                                    getView().showIsBand(bean);
                                }else{
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
